package multisistema;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JViewport;
import javax.swing.table.DefaultTableModel;
import multisistema.objetos.Transaccion;

public class Transacciones extends javax.swing.JPanel {

    public Principal main;
    DefaultTableModel modelo;
    List<Transaccion> lista;
    String[] campos;

    /**
     * Creates new form Miembros
     */
    public Transacciones(Principal prin) {
        main = prin;
        initComponents();
        modelo = (DefaultTableModel) Tabla.getModel();
        modelo.setColumnIdentifiers(new Object[]{"Miembro", "Tipo", "Fecha", "Monto", "Descripcion", "Operacion"});
        campos = new String[]{"nombre_persona", "tipo", "fecha_transaccion", "monto", "descripcion", "operacion"};
        llenarTabla();
        for (int i = 0; i < Tabla.getColumnModel().getColumnCount(); i++) {
            Tabla.getColumnModel().getColumn(i).setMinWidth(100);
        }
    }

    public void volver() {
        this.removeAll();
        this.add(Base, BorderLayout.CENTER);
        this.validate();
        this.repaint();
    }

    public void actualizar() {
        llenarTabla();
    }

    private void llenarTabla() {
        lista = Transaccion.getTransacciones();
        mostarTabla();
    }

    private void mostarTabla() {
        modelo.getDataVector().removeAllElements();
        for (int i = 0; i < lista.size(); i++) {
            modelo.addRow(new Object[]{lista.get(i).getId_persona().getNombre(), lista.get(i).getTipo(),
                lista.get(i).getFecha_transaccion(), lista.get(i).getMonto_de_transaccion(), lista.get(i).getDescripcion(),
                lista.get(i).getId_operacion()});
        }
        this.validate();
        this.repaint();
    }

    private void setDetail(Transaccion m) {
        nombre.setText(m.getId_persona().getNombre());
        tipo.setText(m.getTipo());
        fecha.setText(m.getFecha_transaccion());
        monto.setText(m.getMonto_de_transaccion());
        desc.setText(m.getDescripcion());
        oper.setText(m.getId_operacion());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Base = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tabla = new javax.swing.JTable(){
            // when the viewport shrinks below the preferred size, stop tracking the viewport width
            @Override
            public boolean getScrollableTracksViewportWidth() {
                if (autoResizeMode != AUTO_RESIZE_OFF) {
                    if (getParent() instanceof JViewport) {
                        return (((JViewport)getParent()).getWidth() > getPreferredSize().width);
                    }
                }
                return false;
            }

            // when the viewport shrinks below the preferred size, return the minimum size
            // so that scrollbars will be shown
            @Override
            public Dimension getPreferredSize() {
                if (getParent() instanceof JViewport) {
                    if (((JViewport)getParent()).getWidth() < super.getPreferredSize().width) {
                        return getMinimumSize();
                    }
                }

                return super.getPreferredSize();
            }
        };
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        nombre = new javax.swing.JLabel();
        tipo = new javax.swing.JLabel();
        fecha = new javax.swing.JLabel();
        monto = new javax.swing.JLabel();
        desc = new javax.swing.JLabel();
        oper = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        buscar = new javax.swing.JTextField();
        campos_lista = new javax.swing.JComboBox();
        jButton7 = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(1000, 600));
        setPreferredSize(new java.awt.Dimension(1000, 600));
        setLayout(new java.awt.BorderLayout());

        Base.setLayout(new java.awt.BorderLayout());

        jPanel1.setMinimumSize(new java.awt.Dimension(800, 500));
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 500));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel3.setMinimumSize(new java.awt.Dimension(400, 500));
        jPanel3.setPreferredSize(new java.awt.Dimension(400, 500));
        jPanel3.setRequestFocusEnabled(false);
        jPanel3.setLayout(new java.awt.BorderLayout());

        Tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        Tabla.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        Tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                TablaMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(Tabla);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        jPanel4.setMinimumSize(new java.awt.Dimension(400, 500));
        jPanel4.setPreferredSize(new java.awt.Dimension(400, 500));

        jLabel4.setText("Monto");

        jLabel2.setText("Fecha");

        jLabel3.setText("Tipo");

        jLabel8.setText("Operacion");

        jLabel7.setText("Descripcion");

        jLabel1.setText("Nombre");

        jLabel9.setText("Detalles del Miembro");

        nombre.setText(" ");

        tipo.setText(" ");

        fecha.setText(" ");

        monto.setText(" ");

        desc.setText(" ");

        oper.setText(" ");

        jButton6.setText("Listar Todos");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombre)
                            .addComponent(tipo)
                            .addComponent(fecha)
                            .addComponent(monto)
                            .addComponent(desc)
                            .addComponent(oper)))
                    .addComponent(jLabel9)
                    .addComponent(jButton6))
                .addContainerGap(245, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel9)
                .addGap(47, 47, 47)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nombre))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tipo))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(fecha))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(monto))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(desc))
                .addGap(27, 27, 27)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(oper))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 175, Short.MAX_VALUE)
                .addComponent(jButton6)
                .addContainerGap())
        );

        jPanel1.add(jPanel4, java.awt.BorderLayout.EAST);

        Base.add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setMinimumSize(new java.awt.Dimension(800, 100));
        jPanel2.setPreferredSize(new java.awt.Dimension(800, 100));

        jButton1.setText("Agregar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Volver");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Buscar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Modificar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Eliminar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        campos_lista.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Miembro", "Tipo", "Fecha", "Monto", "Descripcion", "Operacion" }));

        jButton7.setText("Reporte");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton5)
                .addGap(18, 18, 18)
                .addComponent(jButton7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 308, Short.MAX_VALUE)
                .addComponent(campos_lista, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addGap(45, 45, 45))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campos_lista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        Base.add(jPanel2, java.awt.BorderLayout.SOUTH);

        add(Base, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        main.setMain();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        main.setMiembros();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void TablaMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaMousePressed
        int idx = Tabla.getSelectedRow();
        if (!(idx < 0)) {
            setDetail(lista.get(idx));
        }
    }//GEN-LAST:event_TablaMousePressed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        lista = Transaccion.buscar(campos[campos_lista.getSelectedIndex()], buscar.getText());
        mostarTabla();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if (Tabla.getSelectedRow() != -1) {
            this.removeAll();
            this.add(new AdicionarIngresosEgresos(main, lista.get(Tabla.getSelectedRow())), BorderLayout.CENTER);
            this.validate();
        } else {
            JOptionPane.showMessageDialog(main, "Selecione la Transaccion a modificar.", "Error", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        if (Tabla.getSelectedRow() != -1) {
            Transaccion elm = lista.get(Tabla.getSelectedRow());
            int ans = JOptionPane.showConfirmDialog(main, "Esta seguro que desea eliminar la transaccion realizada por  " + elm.getId_persona().getNombre(), "Eliminar?", JOptionPane.OK_CANCEL_OPTION);
            if (ans == 0) {
                elm.eliminar();
                llenarTabla();
            }
        } else {
            JOptionPane.showMessageDialog(main, "Selecione la Transaccion a eliminar.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        llenarTabla();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        PrintWriter out = null;
        File html = new File("report" + System.currentTimeMillis() + ".html");
        try {
            out = new PrintWriter(html);
            out.println("<html><head><title>Reporte de Transaaciones</title><style type=\"text/css\">th{min-width:150px; text-align:left;}</style></head><body><br><br><h1>Reporte de Transacciones</h1><br><br><table>");
            out.println("<th>Miembro</th><th>Tipo</th><th>Fecha</th><th>Monto</th><th>Descripcion</th><th>Operacion</th>");
            for (int i = 0; i < lista.size(); i++) {
                out.println("<tr>");
                out.println("<td>" + lista.get(i).getId_persona().getNombre() + "</td>");
                out.println("<td>" + lista.get(i).getTipo() + "</td>");
                out.println("<td>" + lista.get(i).getFecha_transaccion() + "</td>");
                out.println("<td>" + lista.get(i).getMonto_de_transaccion() + "</td>");
                out.println("<td>" + lista.get(i).getDescripcion() + "</td>");
                out.println("<td>" + lista.get(i).getId_operacion() + "</td>");
                out.println("</tr>");
            }
            out.println("</table></body></html>");
            Portada.openWebpage(html.getAbsolutePath());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Transacciones.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
        
    }//GEN-LAST:event_jButton7ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Base;
    private javax.swing.JTable Tabla;
    private javax.swing.JTextField buscar;
    private javax.swing.JComboBox campos_lista;
    private javax.swing.JLabel desc;
    private javax.swing.JLabel fecha;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel monto;
    private javax.swing.JLabel nombre;
    private javax.swing.JLabel oper;
    private javax.swing.JLabel tipo;
    // End of variables declaration//GEN-END:variables
}
